<template>
  <div>
    <a-form class="search-form" :form="form">
      <a-row :gutter="gutter">
        <a-col :lg="8" :xl="5" :xxl="4">
          <a-form-item label="大区">
            <a-select style="width: 100%" v-model="searchParams.area_id">
              <a-select-option
                v-for="item in examineData"
                :key="item.value"
                :value="item.value"
              >{{item.label}}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :lg="16" :xl="8" :xxl="7">
          <a-form-item label="入驻时间">
            <a-range-picker :value="time" @change="dateChange" style="width: 100%"/>
          </a-form-item>
        </a-col>
        <a-col :lg="14" :xl="11" :xxl="8">
          <a-form-item label>
            <a-select style="width: 40%" v-model="searchParams.search_type">
              <a-select-option
                v-for="item in searchmode"
                :key="item.value"
                :value="item.value"
              >
                {{item.label}}
              </a-select-option>
            </a-select>
            <a-input style="width: 60%" v-model.trim="searchParams.name" placeholder="请输入查询条件"/>
          </a-form-item>
        </a-col>
        <a-col :lg="10" :xl="6" :xxl="5">
          <div class="top-btns">
            <a-button @click="searrchList" type="primary" html-type="submit">查询</a-button>
            <a-button @click="resetcondition">重置</a-button>
          </div>
        </a-col>
      </a-row>
    </a-form>
    <div class="search-result-list">
      <a-table
        :columns="columns"
        :rowKey="({uuid}) => uuid"
        :dataSource="columnsData"
        :pagination="pagination"
        :scroll="scrollX"
        :loading="loading"
      >
        <template slot="is_commission" slot-scope="text, record">
          <a-switch checkedChildren="开" unCheckedChildren="关" :checked="text" disabled/>
        </template>
        <template slot="is_enable" slot-scope="text, record">
          <a-switch checkedChildren="开" unCheckedChildren="关" :checked="text" disabled/>
        </template>
        <template slot="operation" slot-scope="text, record">
          <a @click="() => goDetail(record.id)">详情</a>
        </template>
      </a-table>
      <pagination
        :total="total"
        @loadDataList="getDefaultList"
        ref="pageBand"
      />
    </div>
  </div>
</template>

<script>
import { CompaniesList } from '../../store/regionApi'
import { isPhone, isNum } from '../../utils/check'
export default {
  data() {
    return {
      detailData: {},
      auditTitle: '',
      time: [],
      examineData: [
        {
          value: 0,
          label: '全部'
        },
        {
          value: 1,
          label: '华东'
        },
        {
          value: 2,
          label: '华北'
        },
        {
          value: 3,
          label: '华南'
        },
        {
          value: 4,
          label: '华中'
        },
        {
          value: 5,
          label: '东北'
        },
        {
          value: 6,
          label: '西北'
        },
        {
          value: 7,
          label: '西南'
        },
        {
          value: 8,
          label: '港澳台'
        }
      ],
      searchmode: [
        {
          value: 0,
          label: '全部'
        },
        {
          value: 2,
          label: '大区分公司id'
        },
        {
          value: 3,
          label: '大区分公司名称'
        },
        {
          value: 4,
          label: '大区分公司手机号'
        }
      ],
      searchParams: {
        start_time: 0,
        end_time: 0,
        area_id: 0,
        search_type: 0,
        name: ''
      },
      form: this.$form.createForm(this),
      pagination: false,
      total: 0,
      loading: false,
      gutter: {
        xs: 8,
        sm: 16,
        md: 16,
        lg: 24,
        xl: 24,
        xxl: 48
      },
      scrollX: { x: 0 },
      columnsData: [],
      columns: [
        {
          title: '序号',
          dataIndex: 'listId'
        },
        {
          title: '大区分公司id',
          dataIndex: 'region_no'
        },
        {
          title: '大区分公司名称',
          dataIndex: 'region_name'
        },
        {
          title: '大区分公司手机号',
          dataIndex: 'phone'
        },
        {
          title: '保证金（元）',
          dataIndex: 'deposit'
        },
        {
          title: '余额（元）',
          dataIndex: 'balance'
        },
        {
          title: '大区',
          dataIndex: 'area'
        },
        {
          title: '入驻时间',
          dataIndex: 'created_at'
        },
        {
          title: '分润状态',
          dataIndex: 'is_commission',
          scopedSlots: { customRender: 'is_commission' }
        },
        {
          title: '账号状态',
          dataIndex: 'is_enable',
          scopedSlots: { customRender: 'is_enable' }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' }
        }
      ]
    }
  },
  methods: {
    // 详情
    goDetail(id) {},
    // 重置查询条件
    resetcondition() {
      this.time = []
      // this.current = 1
      // this.pages = {
      //   page: 1,
      //   num: 10
      // }
      this.$refs.pageBand.getData()
      this.searchParams = {
        start_time: 0,
        end_time: 0,
        area_id: 0,
        search_type: 0,
        name: ''
      }
    },
    // 查询
    searrchList() {
      let { search_type, name } = this.searchParams
      if (name && search_type !== 0 && search_type !== 3) {
        let message = ''
        if (search_type === 1 || search_type === 4) {
          if (!isNum(name)) {
            if (search_type === 1) message = '大区分公司id只能是数字'
            if (search_type === 4) message = '手机号只能是数字'
            this.$message.warning(message)
            return false
          }
        }
      }
      this.$refs.pageBand.getData()
    },
    dateChange(date, [start_time, end_time]) {
      this.time = date
      this.searchParams.start_time =
        new Date(start_time + ' 00:00:00').getTime() / 1000
      this.searchParams.end_time =
        new Date(end_time + ' 23:59:59').getTime() / 1000
    },
    getDefaultList(info) {
      // debugger
      this.loading = true
      console.log(info)
      CompaniesList({
        ...this.searchParams,
        page: info.page,
        num: info.pageSize
        })
        .then(res => {
          this.loading = false
          this.columnsData = res.data.list.map((item, index) => {
            item.listId = (info.page - 1) * info.pageSize + (index + 1)
            return item
          })
          this.total = res.data.total_num
        })
        .catch(() => {
          this.loading = false
        })
    },
    // 分页方法
    // pagesSearch(pageNum, pageSize) {
    //   console.log(pageNum, pageSize)
    //   this.current = pageNum
    //   this.pages = {
    //     page: pageNum,
    //     num: pageSize
    //   }
    //   this.getDefaultList()
    // }
  }
}
</script>

<style>
.interval {
  padding: 0 5px;
}
.col-sql {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 130px;
}
</style>